
use "${datadir_psd}\PSD_001_all", clear

keep if yearm_date >= ym(2013, 4)


*--- Region: Keep only England
keep if region < 10


// Save for analysis that includes remortgages
save "${datadir}\PSD_after_2013_03", replace



*--- Keep FTB (first time buyer), STB (home movers) 
drop if advtype!=3 & advtype!=4 


*--- Keep subset of variables 

keep  	date year quarter yearq_date yearm_date											/// dates 
		region																			/// sample restriction
		postcode6 postcode3 yearbirth 													/// panel var
		advtype gross_income impaired age_borrower employment income_basis				/// borrower chars
		property_val new_dwelling purchase_price dwelling_type num_bedrooms				/// house chars
		lender_id lender_name															/// lender chars
		LTV LTI loan_val interest lenderfees repayment ratetype dealtype mortgage_term	/// mortgage chars
		gov_supported_initiative governmentsupportedinitiativeyes						///	HTB
		basic_pay other_employment_income self_employed_income other_income				/// income details
		main_borrower_net_income second_borrower_net_income other_borrowers_net_income	/// income details
		totalcreditcommitments totalmonthlyexpenditure basichouseholdexpenditure		/// expenditure and credit commt
		monthlypaymentforcommittedexpend stress_rate amountofconsolidateddebt			/// expenditure and credit commt
		psd_id 	 																		/// unique identifier	
		saleschannel																	///
		num_bedrooms dwelling_type 														// property characteristics

		
*--- Identify HTB 

/* government support */
gen gov 		= (gov_supported_initiative == 1)

/* ltv */
gen LTV_round	= 0.5 * ceil(LTV/0.5)
gen LTV75 		= (LTV_round==75 | LTV_round==75.5 | LTV_round==76)
gen LTV95 		= (LTV_round==95 | LTV_round==95.5 | LTV_round==96)

/* new dwelling*/
gen new 		= (new_dwelling==0)   // "0" corresponds to "Yes" in the data



*--- Variable for merge later 

gen 		pcd           	= itrim(postcode6)
replace 	pcd 			= regexr(pcd," ","") // quite slow as a command
gen      	purchaseprice 	= cond(mi(purchase_price) | purchase_price == 0, ///
								property_val, purchase_price)
gen      	datey 			= year


*--- Duplicates 
		
bysort  pcd purchaseprice lender_id 								/// matching var
		yearbirth 													/// panel var
		loan_val interest lenderfees repayment ratetype dealtype	/// mortgage chars	
		yearm_date													/// borrowers 
		(gov_supp):	keep if _n == 1



*--- Save 
save "${datadir}\HTB_full", replace
